<?php
/**
 * Index.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 上海牛之云网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com

 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */
namespace addon\supermember\api\controller;

use addon\platformcoupon\model\MemberPlatformcoupon;
use addon\supermember\model\Config;
use app\api\controller\BaseApi;
use app\model\goods\Goods;
use app\model\member\MemberLevel;
use addon\supermember\model\MemberCard as MemberCardModel;


/**
 * 会员卡
 * @package app\api\controller
 */
class Membercard extends BaseApi
{
    /**
     * 列表信息
     */
    public function lists()
    {
        $member_level_model = new MemberLevel();

        $condition = [
            ['level_type', '=', 1],
            ['status', '=', 1]
        ];
        $field = 'level_id,level_name,cover,remark,consume_discount,is_free_shipping,point_feedback,send_point,send_balance,send_coupon,charge_rule,charge_type,bg_color';
        $member_level_list  = $member_level_model->getMemberLevelList($condition, $field, 'is_recommend desc,level_id desc');
        foreach ($member_level_list['data'] as $k=>$v){
            $member_level_list['data'][$k]['charge_rule'] = json_decode($v['charge_rule'],true);
        }
        return $this->response($member_level_list);
    }

    /**
     * 获取会员卡信息
     */
    public function info(){
        $level_id = $this->params['level_id'] ?? 5;

        $member_level_model = new MemberLevel();
        $condition = [
            ['level_type', '=', 1],
            ['level_id', '=', $level_id]
        ];
        $field = 'level_id,level_name,consume_discount,cover,first_content_description,second_content_description,level_price,remark,first_remark,second_remark';
        $data = $member_level_model->getMemberLevelInfo($condition, $field);
        //$data['data']['charge_rule'] = json_decode($data['data']['charge_rule'],true);
        //$coupon = new MemberPlatformcoupon();
        //$coupon_info = $coupon->get
        return $this->response($data);
    }


    /**
     * 开卡协议
     */
    public function agreement(){
        $config = new Config();
        $data = $config->getMemberCardDocument(0);
        return $this->response($data);
    }

    /**
     * Notes:会员开卡列表
     * User: gai
     * Date: 2023/5/11
     * Time: 18:35
     */
    public function openCardLists(){
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $page = isset($this->params['page']) ? $this->params['page'] : 1;
        $page_size = isset($this->params['page_size']) ? $this->params['page_size'] : PAGE_LIST_ROWS;

        $member_level_model = new MemberLevel();
        $alias = 'a';

        $join = [
            [ 'shop s', 'a.site_id = s.site_id', 'inner' ],
            [ 'member_level ml', 'ml.level_id = a.after_level_id', 'inner' ]
        ];
        $condition = [
            [ 'a.member_id', '=', $this->member_id ]
        ];
        $field = 'a.*,s.site_name,s.longitude,s.latitude,ml.cover';
        $order = 'a.change_time desc';
        $res = $member_level_model->getMemberLevelRecordPageList($condition,$page,$page_size,$field,$order,$alias,$join);

        foreach ($res['data']['list'] as $k=>$v){
            $res['data']['list'][$k]['level_expire_time'] = date('Y-m-d',$v['level_expire_time']);
        }
        return $this->response($res);
    }
}